﻿$(document).ready(function () {
    if ($("#ctl00_ContentPlaceHolder3_p_Mail_Merge_Source_ID").val() != "")
        setEmailFieldFromAjax($("#ctl00_ContentPlaceHolder3_p_Mail_Merge_Source_ID").val());
});

var ajaxlist = [];
function split(val) { return val.split(/,\s*/); }
function extractLast(term) { return split(term).pop(); }

function setEmailFieldFromSource(ddlObjVal) {
    try {
        ajaxlist = [];
        if (ddlObjVal.value != "") {
            document.getElementById("ctl00_ContentPlaceHolder3_lblMessage").innerHTML = "";
            document.getElementById("ctl00_ContentPlaceHolder3_p_Mail_Merge_From_Mail").value = "";
            document.getElementById("ctl00_ContentPlaceHolder3_p_Mail_Merge_To_Mail").value = "";
            setEmailFieldFromAjax(ddlObjVal.value);
        }
    }
    catch (err) {
        document.getElementById("ctl00_ContentPlaceHolder3_lblMessage").innerHTML = "<div class=\"mbMessage\"><img id=\"imgInfo\" src=\"Images/info.gif\" alt=\"Information\" /> &nbsp;The selected column name does not have a value associated.</div>";
    }
}

function setEmailFieldFromAjax(ddlObjVal) {
    try {
        ajaxlist = [];
        if (ddlObjVal != "") {
            document.getElementById("ctl00_ContentPlaceHolder3_hfAjaxEmailFieldList").value = "";
            $.ajax({
                url: "mb_ajaxhandler.aspx?action=mail_merge&data=" + ddlObjVal + "&val=",
                dataType: "json",
                cache: false,
                beforeSend: function () { },
                success: function (msg) {
                    for (var i = 0; i < msg.length; i++) {
                        ajaxlist.push(msg[i]["label"]);
                    }
                    document.getElementById("ctl00_ContentPlaceHolder3_hfAjaxEmailFieldList").value = JSON.stringify(msg);
                    //response(msg); 
                },
                complete: function () { },
                error: function (msg) { document.getElementById("ctl00_ContentPlaceHolder3_lblMessage").innerHTML = "<div class=\"mbMessage\"><img id=\"imgInfo\" src=\"Images/info.gif\" alt=\"Information\" /> &nbsp;Select a valid column name.</div>"; }
            });
            $("#ctl00_ContentPlaceHolder3_p_Mail_Merge_From_Mail").each(function () {
                $("#ctl00_ContentPlaceHolder3_p_Mail_Merge_From_Mail").autocomplete({
                    minLength: 3,
                    search: function () { // custom minLength
                        var term = extractLast(this.value);
                        if (term.length < 3) { return false; }
                    },
                    source: function (request, response) {
                        response($.ui.autocomplete.filter(
                            ajaxlist, extractLast(request.term)
                        ));
                    },
                    focus: function () { return false; }, // prevent value inserted on focus
                    select: function (event, ui) {
                        this.value = ui.item.label;
                        return true;
                    }
                });
            });
            $("#ctl00_ContentPlaceHolder3_p_Mail_Merge_To_Mail").each(function () {
                $("#ctl00_ContentPlaceHolder3_p_Mail_Merge_To_Mail").autocomplete({
                    minLength: 3,
                    search: function () { // custom minLength
                        var term = extractLast(this.value);
                        if (term.length < 3) { return false; }
                    },
                    source: function (request, response) {
                        response($.ui.autocomplete.filter(
                            ajaxlist, extractLast(request.term)
                        ));
                    },
                    focus: function () { return false; }, // prevent value inserted on focus
                    select: function (event, ui) {
                        this.value = ui.item.label;
                        return true;
                    }
                });
            });
        }
    }
    catch (err) {
        document.getElementById("ctl00_ContentPlaceHolder3_lblMessage").innerHTML = "<div class=\"mbMessage\"><img id=\"imgInfo\" src=\"Images/info.gif\" alt=\"Information\" /> &nbsp;The selected column name does not have a value associated.</div>";
    }
}

function disableConfirmMsg() {
    document.getElementById("ctl00_ContentPlaceHolder3_btnSave").disabled = false;
    document.getElementById("ctl00_ContentPlaceHolder3_btnSave_top").disabled = false;
    document.getElementById("ctl00_ContentPlaceHolder3_btnSave").value = "Save";
    document.getElementById("ctl00_ContentPlaceHolder3_btnSave_top").value = "Save";
    $("#ctl00_ContentPlaceHolder3_pnl_confirm").css("display", "none"); 
    $("#ctl00_ContentPlaceHolder3_pnl_confirm1").css("display", "none");
}

function enableConfirmMsg() {
    document.getElementById("ctl00_ContentPlaceHolder3_btnSave").disabled = true;
    document.getElementById("ctl00_ContentPlaceHolder3_btnSave_top").disabled = true;
    document.getElementById("ctl00_ContentPlaceHolder3_btnSave").value = "Saving...";
    document.getElementById("ctl00_ContentPlaceHolder3_btnSave_top").value = "Saving...";
    $("#ctl00_ContentPlaceHolder3_pnl_confirm").css("display", "block");
    $("#ctl00_ContentPlaceHolder3_pnl_confirm1").css("display", "block");
    return false;
}
